package com.xmh.test.action;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xmh.test.dao.LoginTestDao;
import com.xmh.test.dao.TestDao;

@Controller()
@RequestMapping("/loginTest")
public class LoginTestAction {

	@Resource(name = "testDaoImple")
	private TestDao testdao;

	@Resource
	private LoginTestDao loginTestDao;

	@RequestMapping("/loginPage")
	public String loginPage(HttpServletRequest request, HttpServletResponse response, String actionUrl) {
		String string = "index.html?actionUrl=" + actionUrl + "&test=222";
		return string;
	}

	@RequestMapping("/loginAction")
	public String loginAction(HttpServletRequest request, HttpServletResponse response, String user, String password,
			String actionUrl) {
		Map<String, Object> userByPass = loginTestDao.getUserByPass(Integer.parseInt(user), password);

		if (userByPass != null) {
			request.getSession().setAttribute("username", user);

			if (StringUtils.isEmpty(actionUrl)) {
				return "loginMain";
			}
			try {
				response.sendRedirect("http://localhost:8082/SSM" + actionUrl);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return "index";
	}

	@RequestMapping("/loginMain")
	public String loginMain(HttpServletRequest request, HttpServletResponse response, String user, String password) {

		return "loginMain";
	}

}
